#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys, os

from loguru import logger

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QDialog
from PyQt5.QtPrintSupport import QPageSetupDialog, QPrinter, QPrintDialog

from qt_material import apply_stylesheet

sys.path.append(os.getcwd())
from views.widgets import Ui_print_setting

class PrintSettingForm(QWidget, Ui_print_setting.Ui_PrintSetting):
    def __init__(self, parent = None):
        super(PrintSettingForm, self).__init__(parent)
        self.setupUi(self)

        self.printer = QPrinter()

        self.parentMainWindow = None
        self.ReturnHomeButton.clicked.connect(self.returnHome)

        self.printSettingButton.clicked.connect(self.showPrintSettingDialog)
        self.printButton.clicked.connect(self.showPrintDialog)

        logger.debug("create an instance of PrintSettingForm successfully")     

    def setMainWindow(self, MainWindow):
        self.parentMainWindow = MainWindow
    
    def returnHome(self):
        if self.parentMainWindow is not None:
            self.parentMainWindow.returnHome()
        else:
            logger.warning("there is no parent main window and cannot return to MainWindow")
    
    def showPrintSettingDialog(self):
        printSettingDialog = QPageSetupDialog(self.printer, self)
        printSettingDialog.exec()
    
    def showPrintDialog(self):
        printDialog = QPrintDialog(self.printer, self)
        if QDialog.Accepted == printDialog.exec():
            logger.debug("print dialog")
            #self.editor.print(self.printer)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    PAWin = PrintSettingForm()
    apply_stylesheet(app, theme = "dark_teal.xml")
    PAWin.show()
    sys.exit(app.exec_())